package com.example.util;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.sys.entity.AsyncRoutes;
import com.example.sys.entity.Dept;
import com.example.sys.entity.Menu;
import com.example.sys.vo.AsyncRoutesVo;
import com.example.sys.vo.DeptVo;
import com.example.sys.vo.MenuVo;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;


public class BaseUtils {

    /**
     *
     * @param list
     * @param parentId
     * @return
     */
    public List<DeptVo> ArrangementDeptTree(List<Dept> list, Long parentId){
        List<DeptVo> deptList=new ArrayList<>();
        if (list==null) return null;

        // 筛选1级部门
        for (Dept p :list){
            if (p.getParentId() == parentId){
                deptList.add(VoUtil.deptToDeptVo(p));
            }
        }
        //筛选出2级部门
        for (DeptVo dv:deptList){
            List<DeptVo> firstDeptList = new ArrayList<>();
            for (Dept d:list){
                if (d.getParentId()==dv.getDeptId()){
                    firstDeptList.add(VoUtil.deptToDeptVo(d));
                }
            }
            //获取3级部门
            for (DeptVo dept1:firstDeptList){
                List<DeptVo> secondDeptList = new ArrayList<>();
                for (Dept d:list){
                    if (d.getParentId()==dept1.getDeptId()){
                        secondDeptList.add(VoUtil.deptToDeptVo(d));
                    }
                }
                dept1.setChildren(secondDeptList);
            }
            dv.setChildren(firstDeptList);
        }
        return deptList;

    }

    public List<DeptVo> ArrangementDeptTree(IPage<Dept> ipageDept, List<Dept> depts) {
        //1级部门转成 DeptVo
        List<DeptVo> deptList=new ArrayList<>();
        for (Dept dept: ipageDept.getRecords()) {
            deptList.add(VoUtil.deptToDeptVo(dept));
        }

        //筛选出2级部门
        for (DeptVo dv:deptList){
            List<DeptVo> firstDeptList = new ArrayList<>();
            for (Dept d:depts){
                if (d.getParentId().equals(dv.getDeptId())){
                    firstDeptList.add(VoUtil.deptToDeptVo(d));
                }
            }
            //获取3级部门
            for (DeptVo deptVo1:firstDeptList){
                List<DeptVo> secondDeptList = new ArrayList<>();
                for (Dept d:depts){
                    if (d.getParentId().equals(deptVo1.getDeptId())){
                        secondDeptList.add(VoUtil.deptToDeptVo(d));
                    }
                }
                for (DeptVo deptVo2:secondDeptList) {
                    List<DeptVo> threeDeptList = new ArrayList<>();
                    for (Dept d:depts) {
                        if (d.getParentId().equals(deptVo2.getDeptId())){
                            threeDeptList.add(VoUtil.deptToDeptVo(d));
                        }
                    }
                    deptVo2.setChildren(threeDeptList);
                }
                deptVo1.setChildren(secondDeptList);
            }
            dv.setChildren(firstDeptList);
        }
        return deptList;
    }

    public List<MenuVo> ArrangementMenuTree(List<Menu> menus, Long parentId) {
        List<MenuVo> menuList=new ArrayList<>();
        if (menus==null) return null;

        // 筛选1级部门
        for (Menu p :menus){
            if (p.getParentId() == parentId){
                menuList.add(VoUtil.menuToMenuVo(p));
            }
        }
        //筛选出2级部门
        for (MenuVo dv:menuList){
            List<MenuVo> firstMenuList = new ArrayList<>();
            for (Menu d:menus){
                if (d.getParentId()==dv.getMenuId()){
                    firstMenuList.add(VoUtil.menuToMenuVo(d));
                }
            }
            //获取3级部门
            for (MenuVo dept1:firstMenuList){
                List<MenuVo> secondMenuList = new ArrayList<>();
                for (Menu d:menus){
                    if (d.getParentId()==dept1.getMenuId()){
                        secondMenuList.add(VoUtil.menuToMenuVo(d));
                    }
                }
                dept1.setChildren(secondMenuList);
            }
            dv.setChildren(firstMenuList);
        }
        return menuList;
    }

    public List<AsyncRoutesVo> ArrangementAsyncRoutesTree(List<AsyncRoutes> asyncRoutes, Long parentId) {
        List<AsyncRoutesVo> menuList=new ArrayList<>();
        if (asyncRoutes==null) return null;

        // 筛选1级部门
        for (AsyncRoutes p :asyncRoutes){
            if (p.getParentId() == parentId){
                menuList.add(VoUtil.asyncRoutesToAsyncRoutesVo(p));
            }
        }
        //筛选出2级部门
        for (AsyncRoutesVo dv:menuList){
            List<AsyncRoutesVo> firstMenuList = new ArrayList<>();
            for (AsyncRoutes d:asyncRoutes){
                if (d.getParentId()==dv.getId()){
                    firstMenuList.add(VoUtil.asyncRoutesToAsyncRoutesVo(d));
                }
            }
            //获取3级部门
            for (AsyncRoutesVo dept1:firstMenuList){
                List<AsyncRoutesVo> secondMenuList = new ArrayList<>();
                for (AsyncRoutes d:asyncRoutes){
                    if (d.getParentId()==dept1.getId()){
                        secondMenuList.add(VoUtil.asyncRoutesToAsyncRoutesVo(d));
                    }
                }
                dept1.setChildren(secondMenuList);
            }
            dv.setChildren(firstMenuList);
        }
        return menuList;
    }

    public List<MenuVo> ArrangementMenuTree(IPage<Menu> ipageMenu, List<Menu> menus) {
        //1级部门转成 MenuVo
        List<MenuVo> menuList=new ArrayList<>();
        for (Menu menu: ipageMenu.getRecords()) {
            menuList.add(VoUtil.menuToMenuVo(menu));
        }

        //筛选出2级部门
        for (MenuVo dv:menuList){
            List<MenuVo> firstMenuList = new ArrayList<>();
            for (Menu d:menus){
                if (d.getParentId().equals(dv.getMenuId())){
                    firstMenuList.add(VoUtil.menuToMenuVo(d));
                }
            }
            //获取3级部门
            for (MenuVo menu1:firstMenuList){
                List<MenuVo> secondMenuList = new ArrayList<>();
                for (Menu d:menus){
                    if (d.getParentId().equals(menu1.getMenuId())){
                        secondMenuList.add(VoUtil.menuToMenuVo(d));
                    }
                }
                for (MenuVo menu2:secondMenuList) {
                    List<MenuVo> threeMenuList = new ArrayList<>();
                    for (Menu d:menus) {
                        if (d.getParentId().equals(menu2.getMenuId())){
                            threeMenuList.add(VoUtil.menuToMenuVo(d));
                        }
                    }
                    menu2.setChildren(threeMenuList);
                }
                menu1.setChildren(secondMenuList);
            }
            dv.setChildren(firstMenuList);
        }
        return menuList;
    }

    /**
     * 传入json对象，将其键转换成驼峰命令规则，值不变
     * @param json
     */
    public  static void convert(Object json) {
        if (json instanceof JSONArray) {
            JSONArray arr = (JSONArray) json;
            for (Object obj : arr) {
                convert(obj);
            }
        } else if (json instanceof JSONObject) {
            JSONObject jo = (JSONObject) json;
            Set<String> keys = jo.keySet();
            String[] array = keys.toArray(new String[keys.size()]);
            for (String key : array) {
                Object value = jo.get(key);
                String[] key_strs = key.split("_");
                if (key_strs.length > 1) {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < key_strs.length; i++) {
                        String ks = key_strs[i];
                        if (!"".equals(ks)) {
                            if (i == 0) {
                                sb.append(ks);
                            } else {
                                int c = ks.charAt(0);
                                if (c >= 97 && c <= 122) {
                                    int v = c - 32;
                                    sb.append((char) v);
                                    if (ks.length() > 1) {
                                        sb.append(ks.substring(1));
                                    }
                                } else {
                                    sb.append(ks);
                                }
                            }
                        }
                    }
                    jo.remove(key);
                    jo.put(sb.toString(), value);
                }
                convert(value);
            }
        }
    }
}
